#-*- coding: utf-8 -*-

from uturtle import *
from math import pi

def jump(t, length):
	dropPen(t)
	moveForward(t, length)
	usePen(t)

def setInCorner(t):
	turnRight(t, 180)
	jump(t, 190)
	turnRight(t)
	jump(t, 190)
	turnRight(t)

def setInLowerCorner(t):
	turnRight(t, 180)
	jump(t, 190)
	turnLeft(t)
	jump(t, 190)
	turnLeft(t)

def square(t, length):
	for i in range(4):
		moveForward(t, length)
		turnRight(t)

def polygon(t, length, n):
	angle = 360.0 / n
	for i in range(n):
		moveForward(t, length)
		turnRight(t, angle)

def circle(t, r):
	nb_cotes = 300
	long = 2*pi*r/nb_cotes
	polygon(t, long, nb_cotes)

def arc(t, ray, angle):
	nb_cotes = 100
	coin = 360.0 / nb_cotes
	maxAngles = angle/coin
	length = 2*pi*ray/nb_cotes
	while maxAngles:
		moveForward(t, length)
		turnRight(t, coin)
		maxAngles -= 1

def aperture(t, ray, n):
	angle = 360.0 / n
	for i in range(n):
		moveForward(t, ray)
		turnRight(t, angle)
		turnRight(t, angle/2)
		moveForward(t, ray)
		moveBackward(t, ray)
		turnLeft(t, angle/2)

def koch(t, x, seuil):
	x = float(x)
	#print "Koch:", x, seuil
	if x<seuil:
		moveForward(t, x)
	else:
		koch(t, x/3, seuil)
		turnLeft(t, 60)
		koch(t, x/3, seuil)
		turnRight(t, 120)
		koch(t, x/3, seuil)
		turnLeft(t, 60)
		koch(t, x/3, seuil)

def flocon(t, length, n, seuil):
	angle = 360.0 / n
	for i in range(n):
		koch(t, length, seuil)
		turnRight(t, angle)

def carre(t, x, seuil):
	x = float(x)
	#print "Koch:", x, seuil
	if x<seuil:
		moveForward(t, x)
	else:
		carre(t, x/4, seuil)
		turnLeft(t)
		carre(t, x/4, seuil)
		turnRight(t)
		carre(t, x/4, seuil)
		turnRight(t)
		carre(t, x/4, seuil)
		turnLeft(t)
		carre(t, x/4, seuil)

def arbre(t, x, n, a):
	x = float(x)
	moveForward(t, x)
	if n>1:
		turnLeft(t, a)
		arbre(t, x/2, n-1, a)
		turnRight(t, a*2)
		arbre(t, x/2, n-1, a)
		turnLeft(t, a)
	moveBackward(t, x)

def contient(nbre, chiffre):
	while nbre:
		d = nbre % 10
		if chiffre==d:
			return True
		nbre = nbre / 10
	return False

def pgcd(x, y):
	if y==0:
		return x
	return pgcd(y, x%y)

def est_multiple(x, y):
	return pgcd(x, y)==x

def ackermann(m, n):
	if m == 0:
		return n+1
	elif m>0 and n==0:
		return ackermann(m-1, 1)
	else: #m>0 and n>0
		return ackermann(m-1, ackermann(m, n-1))

def trianglePascal(i, j):
	if i==0 and j==0:
		return 1
	#Est-ce qu'on sort du triangle ?
	if i<1 or j>i:
		#Oui : on considère que c'est nul
		return 0
	else:
		#Non : somme de la case au-dessus et au-dessus à gauche
		return trianglePascal(i-1, j)+trianglePascal(i-1, j-1)

def dessin():
	bob = umonsTurtle()
	dropPen(bob)
	turnLeft(bob)
	moveBackward(bob, 100)
	usePen(bob)
	#setInLowerCorner(bob)
	#koch(bob, 1000, 10)
	arbre(bob, 50, 5, 20)
	wait ()


def eval(exp, d=0):
	if d:
		print "eval(", exp, ")"
	#Si on a reçu un nombre en paramètre,
	#on retourne simplement ce nombre.
	if isinstance(exp, int):
		return exp
	elif isinstance(exp, float):
		return exp
	#Sinon, c'est un tuple (a,op,b)
	#On extrait chaque morceau
	a, op, b = exp
	#On calcule les opérandes (VALEURS !)
	valA = eval(a, d)
	valB = eval(b, d)
	#Selon l'opérateur, on effectue l'opération
	if op=='+':
		return valA+valB
	elif op=='-':
		return valA-valB
	elif op=='*':
		return valA*valB
	elif op=='/':
		return valA/valB
	else:
		print "WATTA F**K? C'est quoi,", op, "?"


bob = umonsTurtle()
#print eval((((18, '*', 3), '+', (14, '/', 2)), '-', 1))
#arbre (bob ,100 ,5, 20)
flocon(bob, 1000, 5, 100)
#aperture(bob, 100, 10)
wait ()
